 <template>
  <div>
    <!--搜索查询-->
    <el-row>
      <el-col :span="3">&nbsp;</el-col>
      <el-col :span="3">
        <el-select v-model="searchForm.name" size="mini" placeholder="请选择等级">
          <el-option :key="0" label="请选择等级" value="" ></el-option>
          <el-option v-for="item in optionsA" :key="item.id" :label="item.name" :value="item.name">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="3">

      </el-col>
      <el-col :span="3">

      </el-col>
      <el-col :span="3">

      </el-col>
      <el-col :span="3">
        <el-button type="primary" size="mini" @click="search()" icon="el-icon-search">搜索
        </el-button>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" size="mini" @click="handlerAdd()" icon="el-icon-circle-plus">添加
        </el-button>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" size="mini" @click="flush()" icon="el-icon-search">刷新</el-button>
      </el-col>
    </el-row>

    <el-row>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="name" label="等级名" width="110">
        </el-table-column>
        <el-table-column prop="growthPoint" label="成长值" width="100">
        </el-table-column>
        <el-table-column prop="freeFreightPoint" label="免运费标准" width="120" align="center"> </el-table-column>
        <el-table-column prop="priviledgeComment" label="评论获奖励特权" width="150" align="center" :formatter="statusFormat"> </el-table-column>
        <el-table-column prop="priviledgePromotion" label="专享活动特权 " align="center" :formatter="statusFormat"> </el-table-column>
        <el-table-column prop="priviledgeMemberPrice" label="会员价格特权 " align="center" :formatter="statusFormat"> </el-table-column>
        <el-table-column prop="priviledgeBirthday" label="生日特权 " align="center" width="160" :formatter="statusFormat"> </el-table-column>
        <el-table-column prop="ratio" label="积分" align="center" width="100"> </el-table-column>
        <el-table-column label="操作" width="250" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row>
      <!--分页插件-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="[2, 3, 5, 10, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-row>

    <!--会员等级添加/更新对话框-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="form">
        <el-form-item label="等级名" prop="name" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="成长值" prop="growthPoint" :label-width="formLabelWidth">
          <el-input v-model="form.growthPoint" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="免运费标准" prop="freeFreightPoint" :label-width="formLabelWidth">
          <el-input v-model="form.freeFreightPoint" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="评论获奖励特权" prop="priviledgeComment" :label-width="formLabelWidth">
          <el-select v-model="form.priviledgeComment" placeholder="状态">
            <el-option label="没有" value="0"></el-option>
            <el-option label="有" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专享活动特权" prop="priviledgePromotion" :label-width="formLabelWidth">
          <el-select v-model="form.priviledgePromotion" placeholder="状态">
            <el-option label="没有" value="0"></el-option>
            <el-option label="有" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员价格特权" prop="priviledgeMemberPrice" :label-width="formLabelWidth">
          <el-select v-model="form.priviledgeMemberPrice" placeholder="状态">
            <el-option label="没有" value="0"></el-option>
            <el-option label="有" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生日特权" prop="priviledgeBirthday" :label-width="formLabelWidth">
          <el-select v-model="form.priviledgeBirthday" placeholder="状态">
            <el-option label="没有" value="0"></el-option>
            <el-option label="有" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="积分" prop="ratio" :label-width="formLabelWidth">
          <el-input v-model="form.ratio" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

  <script>
export default {
  data() {
    return {
      optionsA:[],
      imageUrl: '',
      tableData: [],
      currentPage: 1, //页码 第几页
      pageSize: 5, //每页显示数量
      total: 100, //总条数
      dialogFormVisible: false, //是否显示对话框  默认是不显示
      form: { priviledgeComment:'0', priviledgePromotion:'0',priviledgeMemberPrice:'0',priviledgeBirthday:'0',},//form表单对象
      formLabelWidth: '120px',
      searchForm: {
        name: ''
      },//搜索form表单
      title:"",
    };
  },
  created() {
    this.loadData();
    var _this = this;
    //加载添加的等级下拉数据
    this.$http.get('/memberLevel/queryByParam')  //指定请求的url及参数  get
      .then(function (response) {  //正确返回对象的处理
        //JSON.stringify   把json转换为json字符串，方便查看
        console.log(JSON.stringify(response));
        //判断返回值是否正确
        if (response.data.code == 200) {
          _this.optionsA = response.data.data;
        }
      })
  },
  //所有函数
  methods: {
    //加载数据
    loadData() {
      var _this = this;
      //复制 axios官网代码
      // var token  = localStorage.getItem("token");
      this.$http
        .post("/memberLevel/queryByPage", {
          pageNo: this.currentPage,
          pageSize: this.pageSize,
          data: {
            name: this.searchForm.name,
          }
        })
        .then(function (response) {
          if (response.data.code == 200) {
            _this.tableData = response.data.data.records;
            _this.total = response.data.data.total;
          }
        })
        .catch(function (error) {
          console.log(error);
          _this.$message.error("你错了。。。");
        });
    },
    statusFormat(row, column, cellValue, index) {
      return cellValue == 0 ? "没有" : cellValue == 1 ? "有" : "未知";
    },
    //处理每页数量发生变化
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      //让每页数量发生变化
      this.pageSize = val;
      //重载数据
      this.loadData();
    },
    //处理当前页变化
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      //让每页数量发生变化
      this.currentPage = val;
      //重载数据
      this.loadData();
    },
    //查询
    search() {
      this.loadData();
    },
    //刷新
    flush() {
     location.reload();
    },
    //处理添加
    handlerAdd() {
      //显示对话框
      this.dialogFormVisible = true;
      //指定对话框标题
      this.title = '会员添加';
      //让form表单重新赋空值
      this.form = {};
    },
    //处理更新
    handleEdit(index, row) {
      console.log(index, row);
      //显示对话框
      this.dialogFormVisible = true;
      //指定对话框标题
      this.title = '会员更新';
      //把一行数据赋值给form赋值
      this.form = row;
      this.imageUrl = row.avatar
      //把数字1变为字符串1
      this.form.priviledgeComment = row.priviledgeComment + '';
      this.form.priviledgePromotion= row.priviledgePromotion + '';
      this.form.priviledgeBirthday = row.priviledgeBirthday + '';
      this.form.priviledgeMemberPrice = row.priviledgeMemberPrice + '';
    },
    //提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var url = "";
          if (this.title == '会员添加') {
            url = '/memberLevel/add';
          } else {
            url = '/memberLevel/edit';
          }
          //让_this等于当前vue对象
          var _this = this;
          // alert(111);
          //赋值 axios官网代码  this.form =form:{deptName:'',description:'',status:1}
          _this.$http.post(url, this.form)  //指定请求的url及参数  get
            .then(function (response) {  //正确返回对象的处理
              // handle success
              console.log(response);
              //JSON.stringify   把json转换为json字符串，方便查看
              console.log(JSON.stringify(response));
              //直接使用this 会指 axios对象  错误
              // this.tableData
              //判断返回值是否正确
              if (response.data.code == 200) {
                //关闭弹框
                _this.dialogFormVisible = false;
                //提示添加成功
                _this.$message({
                  message: '恭喜你，这是一条成功消息',
                  type: 'success'
                });
                //重新加载数据
                _this.loadData();
              } else {
                //错误提示信息
                _this.$message.error('错了哦，这是一条错误消息');
              }

            })
            .catch(function (error) {//错误的处理
              // handle error
              console.log(error);
            })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //处理删除
    handleDelete(index, row) {
      console.log(index, row);
      //确认框提示
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //让_this等于当前vue对象
        var _this = this;
        _this.$http.delete("/memberLevel/deleteById?id=" + row.id)  //指定请求的url及参数  get
          .then(function (response) {  //正确返回对象的处理
            // handle success
            console.log(response);
            //JSON.stringify   把json转换为json字符串，方便查看
            console.log(JSON.stringify(response));
            //直接使用this 会指 axios对象  错误
            // this.tableData
            //判断返回值是否正确
            if (response.data.code == 200) {
              //提示添加成功
              _this.$message({
                message: '恭喜你，这是一条成功消息',
                type: 'success'
              });
              //重新加载数据
              _this.loadData();
            } else {
              //错误提示信息
              _this.$message.error('错了哦，这是一条错误消息');
            }

          })
          .catch(function (error) {//错误的处理
            // handle error
            console.log(error);
          })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

  },
};
</script>
  <style>
  </style>